from sqlalchemy.orm import Session
import datetime
import models, schemas

'''
本文件负责定义处理数据库的函数
'''

def get_number(db: Session, notice: schemas.NoticeBase):
    # 获得该工厂的所有公告列表
    notices = db.query(models.Notice).filter(models.Notice.factory == notice.factory).filter(models.Notice.year == str(datetime.datetime.today().year)).all()
    # 如果有公告
    if notices:
        last = notices[-1]
    else:
        last = models.Notice(factory=notice.factory,year=str(datetime.datetime.today().year),
                             number="[" + str(datetime.datetime.today().year) + "]0",name=notice.name,title=notice.title,
                             datetime=datetime.datetime.now(), etc="")
    max_num = int(last.number.split(']')[-1])
    max_num = max_num + 1
    new_number = "[" + str(datetime.datetime.today().year) + "]" + str(max_num)
    new_notice = models.Notice(factory=notice.factory,year=str(datetime.datetime.today().year),
                               number=new_number,name=notice.name,title=notice.title,
                               datetime=datetime.datetime.now(),etc="")
    db.add(new_notice)
    db.commit()
    return new_notice

# def insert_notice(db: Session, factory: str, name: str, title: str):
#     db_comment = models.Notice()